﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entities;
using Utility;
using BUS;

public partial class A_LetsDeal_thongtinsanpham : System.Web.UI.Page
{
   public int sogiay = 0;
   public int dem = 0;
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           Session["tranghientai"] = Request.Url.ToString();
           load_data();
       }
   }

    public void load_data()
    {

        int id = int.Parse(Request.QueryString["id"]);
        Tbl_SanPham_Entities sanpham = Tbl_SanPham_BUS.GetByID(id);
        ten_sp.Text = sanpham.TenSP;

        hd_stt.Value = sanpham.STT.ToString();

        ten_sp.Text = sanpham.TenSP;

        lit_giaban.Text = "<br />&nbsp&nbsp&nbsp" + String.Format("{0:0,0 đ}", sanpham.GiaBan);

        lit_giagoc.Text = String.Format("{0:0,0 đ}", sanpham.GiaGoc);

        int sodh = Tbl_CTDH_BUS.CountByIDSP(sanpham.STT);
        lit_vccon.Text = (sanpham.SLBan - sodh).ToString();

        lit_songuoimua.Text = sodh.ToString();

        lit_ngayhethan.Text = sanpham.NgayKetThucDuKien;

        lit_ttsp.Text = sanpham.ThongTinCoBan;

        lit_dksd.Text = sanpham.DKSD;
        if (sanpham.TinhTrang == "Open")
        {
            lbt_dathang.NavigateUrl = "~/A_LetsDeal/giohang.aspx?id=" + sanpham.STT.ToString();
        }
        else
        {
            lbt_dathang.Visible = false;
        }
        if (sanpham.ThongTinDayDu == "")
        {
            lit_ttct.Text = "Không có thông tin chi tiết về sản phẩm";
        }
        else
        {
            lit_ttct.Text = sanpham.ThongTinDayDu;
        }


        Tbl_CTDH_Collection ctdh = Tbl_CTDH_BUS.GetByIDSP(sanpham.STT);
        for (int z = 0; z < ctdh.Count; z++)
        {
            Tbl_DonHang_Entities donhang = Tbl_DonHang_BUS.GetByID(ctdh[z].DonHang);
            Tbl_KhachHang_Entities khachhang = Tbl_KhachHang_BUS.GetByID(donhang.NguoiDat);
            if (z == 0)
                txtdsnguoidat.Items.Add("Danh sách đặt hàng:");
            txtdsnguoidat.Items.Add(txtdsnguoidat.Text + (z + 1).ToString() + "." + khachhang.TaiKhoan + " (" + ctdh[z].SoLuong.ToString() + "vc)");

        }

        Tbl_ImageSP_Collection hinh = Tbl_ImageSP_BUS.demhinh(sanpham.STT);
        Repeater1.DataSource = hinh;
        Repeater1.DataBind();
        

        int adad = hinh.Count;
        DateTime now = DateTime.Now;
        DateTime enddate = DateTime.Parse(sanpham.NgayKetThucDuKien);

        TimeSpan t = enddate - now;
        sogiay = int.Parse((Math.Floor(t.TotalSeconds)).ToString());
        int total = 0;
        Criteria c = new Criteria();
        c.add(Condition.AND, Tbl_SPComment_Columns.SanPham, Condition.EQUAL,id);
        Tbl_SPComment_Collection spcmCollection =
           Tbl_SPComment_BUS.Get(c.Criter, 0, 1,
           Tbl_SPComment_Columns.STT.ToString(), OrderDirection.DESC.ToString(), out total);

        Repeater2.DataSource = spcmCollection;
        Repeater2.DataBind();


    }


    protected void bt_timkiem_Click(object sender, EventArgs e)
    {
        if (txt_timkiem.Text != "")
        {
            Response.Redirect("~/A_LetsDeal/Default.aspx?tensanpham=" + txt_timkiem.Text);
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                 e.Item.ItemType == ListItemType.Item)
        {
            Tbl_ImageSP_Entities hinhanh = (Tbl_ImageSP_Entities)e.Item.DataItem;
            Image imgsp = (Image)e.Item.FindControl("imgsp");
            imgsp.ImageUrl = "~/Photos/Thumbs/" + hinhanh.ImagePath;

        }
    }
    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                         e.Item.ItemType == ListItemType.Item)
        {
            Tbl_SPComment_Entities spcm = (Tbl_SPComment_Entities)e.Item.DataItem;
            Literal lit_cm = (Literal)e.Item.FindControl("lit_cm");
            Tbl_KhachHang_Entities kh = Tbl_KhachHang_BUS.GetByID(spcm.NguoiDang);
            lit_cm.Text = "<font color='red'>" + kh.TaiKhoan + "</font>: " + spcm.Comment + "<br /><font size='-2' color='silver'>" + spcm.NgayDang + "</font>";
            if (Session["username"] != null)
            {
                if (spcm.NguoiDang == Tbl_KhachHang_BUS.GetByUsername(Session["username"].ToString()).STT)
                {
                    HyperLink hyp_xoa = (HyperLink)e.Item.FindControl("hyp_xoa");
                    HyperLink hyp_traloi = (HyperLink)e.Item.FindControl("hyp_traloi");
                    hyp_xoa.Text = "Xóa";
                    hyp_traloi.Text = "";
                    hyp_xoa.NavigateUrl = "~/A_LetsDeal/xoa_cm.aspx?id=" + spcm.STT.ToString();
                    hyp_traloi.NavigateUrl = "";
                }
                else 
                {
                    HyperLink hyp_xoa = (HyperLink)e.Item.FindControl("hyp_xoa");
                    HyperLink hyp_traloi = (HyperLink)e.Item.FindControl("hyp_traloi");
                    hyp_xoa.Text = "";
                    hyp_traloi.Text = "Trả lời";
                    hyp_xoa.NavigateUrl = "";
                    hyp_traloi.NavigateUrl = "#";
                }
            }
            else
            {
                HyperLink hyp_xoa = (HyperLink)e.Item.FindControl("hyp_xoa");
                HyperLink hyp_traloi = (HyperLink)e.Item.FindControl("hyp_traloi");
                hyp_xoa.Text = "";
                hyp_traloi.Text = "";
                hyp_xoa.NavigateUrl = "#";
                hyp_traloi.NavigateUrl = "#";
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Tbl_SPComment_Entities spcm = new Tbl_SPComment_Entities();
        spcm.SanPham = int.Parse(Request.QueryString["id"]);
        spcm.Comment = txt_comment.Text;
        spcm.NgayDang = DateTime.Now.ToString();
        spcm.NguoiDang = Tbl_KhachHang_BUS.GetByUsername(Session["username"].ToString()).STT;
        spcm.CommentCha = true;
        Tbl_SPComment_BUS.Insert(spcm);
        Response.Redirect(Request.RawUrl);
    }
}

